package com.learning.security.core.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("msg", "登录成功");// 打印登录成功信息
        result.put("code", 200);// 打印状态码
        result.put("data", authentication);// 打印认证信息
        response.setContentType("application/json;charset=UTF-8");// 设置响应类型
        String s = new ObjectMapper().writeValueAsString(result);// json格式转字符串
        response.getWriter().println(s);// 打印json格式数据
    }
}
